package com.example.annotation;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.annitaions.SZJ;
import com.example.annitaions.szjFind;
import com.example.annitaions.szjIntent;
import com.example.annotation.annotations.szjOnClick;
import com.example.annotation.annotations.szjOnLongClick;
import com.example.annotation.bean.UserBean;
import com.example.annotation.utils.szjAnnotationUtil;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

@SuppressLint("NonConstantResourceId")
public class MainActivity2 extends AppCompatActivity {

    @SZJ(name = "SZJ", age = 18)
    public int a = 1;

    @SZJ(name = "JZS", age = 17)
    public MainActivity2() {

    }

    //    @SZJ(name = "ZSJ",age = 19)
    public void test() {
        System.out.println("我是方法哦");

        //TODO 对象名获取 class
        UserBean userBean = new UserBean();
        Log.i("szjUserBean",userBean.toString());
        try {
            Class<? extends UserBean> userBeanCls = userBean.getClass();
            Field nameField = userBeanCls.getDeclaredField("name");
            //允许访问 private
            nameField.setAccessible(true);
            //修改状态
            nameField.set(userBean,"SZJ");
            Log.i("szjUserBean",userBean.toString());


            //TODO 全类名获取 class
            Class<?> userBeanCls2 = Class.forName("com.example.annotation.bean.UserBean");
            Field nameField2 = userBeanCls2.getDeclaredField("name");
            nameField2.setAccessible(true);
            //修改状态
            nameField2.set(userBean,"我是SZJ");
            Log.i("szjUserBean",userBean.toString());


            //TODO 反射调用方法
            Method test = userBeanCls.getMethod("test",String.class,int.class);
            String userTestRt = (String) test.invoke(userBean, "张三1", 22);
            Log.i("szjUserBean",userTestRt);


            Method test2 = userBeanCls.getDeclaredMethod("test2",String.class,int.class);
            //允许访问private
            test2.setAccessible(true);
            String userTestRt2 = (String) test2.invoke(userBean, "张三2", 33);
            Log.i("szjUserBean2",userTestRt2);

        } catch (Exception e) {
            e.printStackTrace();
            Log.i("szjError", e.toString());
        }
    }


    @szjIntent
    int id;
    @szjIntent
    String name;
    @szjIntent
    boolean sex;
    @szjIntent("userBean")
    UserBean bean;
    @szjIntent
    boolean sex11;

    @szjFind(R.id.mian2Tv)
    TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        //初始化
        szjAnnotationUtil.getInstance().init(this);

        //打印是否接收到Intent传过来的参数
        tv.setText("id:" + id + "\tname:" + name + "\tsex:" + sex + "\t" + sex11 + "\n" + bean.toString());

        test();
    }

    @szjOnClick({R.id.bt, R.id.mian2Tv})
    public void click(View view) {
        if (view.getId() == R.id.bt) {
            Toast.makeText(this, "点击了", Toast.LENGTH_SHORT).show();
        }
    }


    @szjOnLongClick({R.id.bt, R.id.mian2Tv})
    public boolean longClick(View view) {
        if (view.getId() == R.id.bt) {
            Toast.makeText(this, "长按了", Toast.LENGTH_SHORT).show();
        }
        return false;
    }
}